﻿var pageSize = 15;
//订单列表
function loadList(page, className) {
    if (!toLogin()) return;
    var osn = $("#osn").val();
    var consignee = $("#consignee").val();
    var orderstate = $("#orderstate").val();

    AjaxStart(api_domain + "api/order/orderlist?storeid=" + userid + "&pageSize=" + pageSize + "&pageNumber=" + page + "&osn=" + osn + "&consignee=" + consignee + "&orderstate=" + orderstate, "", function (data) {
        if (data.Data.OrderList == null || data.Data.OrderList.length < 1) {
            scroll_state = false;
            $(".orderlist").append("<div style='text-align:center;'>暂无数据</div>");
            return;
        }
        $(".orderlist").append(getItem(data.Data.OrderList));
        $(".box2").click(function () { toUrl(PageUrl.Order_Detail + "?oid=" + $(this).attr("data-oid") + "&uid=" + $(this).attr("data-uid")) })
    });
}

function getItem(list) {
    var html = "";
    for (var i = 0; i < list.length; i++) {
        var item = list[i];
        //检查是否显示别人的数据  
        if (!checkUserId(item.storeid)) return;

        var address = item.provincename + item.countyname + item.cityname + item.address;
        html += "<div class='box1'>";
        html += "<div class='box2' data-oid='" + item.oid + "' data-uid='" + item.uid + "'>" + item.consignee + " " + address + " " + item.mobile + "</div>";
        html += "<div class='box3'>";
        html += "<div class='box5' style='border-bottom:solid 1px #eaeaea;'>订单金额</div>";
        html += "<div class='box5'>" + item.orderamount + "元</div>";
        html += "</div>";
        html += "<div class='box4' style='padding-top:3px;'>" + item.osn + "</div>";
        html += "<div class='box4' style='position:absolute;left:43.5%;'>" + getState(item.orderstate) + "</div>";
        html += "<div class='box4' style='position:absolute;left:66%;padding-top:2px;'>" + item.addtime.replace("T", " ").substring(0, 19) + "</div>";
        html += "</div>";
    }
    return html;
}

function search() {
    $(".orderlist").html("");
    loadList(1);
}

//订单详情
function loadDetail(uid, oid) {
    if (!toLogin()) return;
    AjaxStart(api_domain + "api/order/getorder?uid=" + uid + "&oid=" + oid, "", function (data) {
        if (data.Data.OrderInfo == null) {
            return;
        }
        loadDetailItem(data);        
    });
}

function loadDetailByOSN(uid, osn) {
    AjaxStart(api_domain + "api/order/getorderByOSN?uid=" + uid + "&osn=" + osn, "", function (data) {
        if (data.Data.OrderInfo == null) {
            return;
        }
        loadDetailItem(data);
    });
}

function loadDetailItem(data) {
    var item = data.Data.OrderInfo;
    //检查是否显示别人的数据  
    if (!checkUserId(item.StoreId)) return;

    $(".osn").html(item.OSN)
    $(".state").html(getState(item.OrderState))
    $(".addtime").html(item.AddTime)
    $(".BuyerRemark").html((item.BuyerRemark == '' ? "暂无" : item.BuyerRemark));
    if (trim(item.Sid) != "") {
        $(".sid").html(item.Sid);
        $(".sid-box").css("display", "");
    }
    
    //金额
    $(".OrderAmount").html("￥" + item.OrderAmount)
    $(".CouponMoney").html(item.CouponMoney)
    $(".PayCreditMoney").html(item.PayCreditMoney)
    $(".SurplusMoney").html("￥" + item.SurplusMoney)
    $(".Discount").html( item.Discount > 0 ? "-" + item.Discount : item.Discount)

    //物流
    $(".ShipCoName").html(item.ShipCoName)
    $(".Consignee").html(item.Consignee)
    $(".Mobile").html(item.Mobile)
    $(".Address").html(item.Address)
    $(".ZipCode").html(item.ZipCode)
    $(".ShipFee").html(item.ShipFee)
    $(".ShipSN").html(item.ShipSN)

    //订单操作
    $(".btn-manage").click(function () { toUrl(PageUrl.Order_Manage + "?oid=" + item.Oid + "&uid=" + item.Uid) });
    $(".btn-action").click(function () { toUrl(PageUrl.Order_Action_List + "?oid=" + item.Oid + "&uid=" + item.Uid) });

    //订单产品
    var html = ""
    var count = 0;
    var prdList = data.Data.OrderProductList;
    for (var i = 0; i < prdList.length; i++) {
        var info = prdList[i].Info;
        var src = "http://static.711688.net.cn/upload/store/" + userid + "/product/show/thumb190_190/" + info.ShowImg;
        count += info.BuyCount;
        html += "<div class='prd'>";
        html += "<div class='left prd1'><img src='" + src + "' /></div>";
        html += "<div class='left prd2'><div class='title'>" + info.Name + "</div></div>"
        html += "<div class='right prd3 align-r'><div class='price red'>￥" + info.ShopPrice + "</div><div class='price'>￥" + info.ShopPrice + "</div><div class='nums'>x" + info.BuyCount + "</div></div>"
        html += "<div class='clear align-r lh35'><span class='OrderAmount'></span></div>";
        if (i == prdList.length - 1)
            html += "<div class='clear bt-line align-r lh35'>共" + count + "件商品 合计：<span class='OrderAmount'>" + item.OrderAmount + "</span></div>";
        html += "</div>";
    }
    
    $(".productList").html(html);

    //操作列表       
    html = "";
    var prdList = data.Data.OrderActionInfo;
    for (var i = 0; i < prdList.length; i++) {
        var info = prdList[i];
        html += "<div class=\"box\"><div class=\"box1 b-left\">操作人</div><div class=\"box1 b-right\">" + info.RealName + "</div><div class='clear'></div></div>"
        html += "<div class=\"box\"><div class=\"box1 b-left\">操作时间</div><div class=\"box1 b-right\">" + info.ActionTime + "</div><div class='clear'></div></div>"
        html += "<div class=\"box\" style=\"border-bottom:solid 8px #e4e4e4;\"><div class=\"box1 b-left\">操作备注</div><div class=\"box1 b-right state lh-25\" style='padding-top: 5px;'>" + info.ActionDes + "</div><div class='clear'></div></div>"
    }
    $(".actionList").html(html);
}


//取消
function order_cacel(oid, uid) {
    if (!toLogin()) return;
    weUI.Confirm("确定要取消订单吗?", function () {
        AjaxStart(api_domain + "/api/Order/CancelOrder", "oid=" + oid + "&uid=" + uid, function (data) {
            if (data.Code != 0) {
                Alert(data.Message)
                return;
            }
            Alert("操作成功");
            setTimeout(function () {
                toUrl(PageUrl.Order_Manage + "?oid=" + oid + "&uid=" + uid);
            }, 1000);
        });
    });
}

//锁定
function order_lock(oid, uid) {
    if (!toLogin()) return;
    weUI.Confirm("确定要锁定订单吗?", function () {
        AjaxStart(api_domain + "/api/Order/LockOrder", "oid=" + oid + "&uid=" + uid, function (data) {
            if (data.Code != 0) {
                Alert(data.Message)
                return;
            }
            Alert("操作成功");
            setTimeout(function () {
                toUrl(PageUrl.Order_Manage + "?oid=" + oid + "&uid=" + uid);
            }, 1000);
           
        });
    })
}

//发货
function send(oid, uid, shipCoId, shipSn) {
    if (!toLogin()) return;
    if (shipCoId < 1) {
        Alert("请选择发货公司")
        return;
    }
    if (shipSn == '') {
        Alert("请填写运单号")
        return;
    }
    AjaxStart(api_domain + "/api/Order/SendOrderProduct", "oid=" + oid + "&uid=" + uid + "&shipCoId=" + shipCoId + "&shipSn=" + shipSn, function (data) {
        if (data.Code != 0) {
            Alert(data.Message)
            return;
        }
        Alert("操作成功");
        setTimeout(function () {
            toUrl(PageUrl.Order_Manage + "?oid=" + oid + "&uid=" + uid);
        }, 1000);
    });
}

//修改订单折扣
function update_discount(oid, uid, discount) {
    if (!toLogin()) return;
    if (discount=='') {
        Alert("请填写折扣")
        return;
    }
    if (isNaN(discount)) {
        Alert("折扣必须是数字")
        return;
    }

    AjaxStart(api_domain + "/api/Order/UpdateOrderDiscount", "oid=" + oid + "&uid=" + uid + "&discount=" + discount, function (data) {
        if (data.Code != 0) {
            Alert(data.Message)
            return;
        }
        Alert("操作成功");
        setTimeout(function () {
            toUrl(PageUrl.Order_Manage + "?oid=" + oid + "&uid=" + uid);
        }, 1000);
    });
}

//更新订单配送费用
function update_shipfee(oid, uid, discount) {
    if (!toLogin()) return;
    if (discount == '') {
        Alert("请填写折扣")
        return;
    }
    if (isNaN(discount)) {
        Alert("折扣必须是数字")
        return;
    }

    AjaxStart(api_domain + "/api/Order/UpdateOrderShipFee", "oid=" + oid + "&uid=" + uid + "&shipFee=" + discount, function (data) {
        if (data.Code != 0) {
            Alert(data.Message)
            return;
        }
        Alert("操作成功");
        setTimeout(function () {
            toUrl(PageUrl.Order_Manage + "?oid=" + oid + "&uid=" + uid);
        }, 1000);
    });
}

function getState(state) {
    if (state == 20)
        return "等待付款";
    else if (state == 40)
        return "待确认";
    else if (state == 60)
        return "已确认";
    else if (state == 80)
        return "备货中";
    else if (state == 100)
        return "已发货";
    else if (state == 120)
        return "已收货";
    else if (state == 140)
        return "锁定";
    else if (state == 160)
        return "取消";
    else
        return "未知状态";
}

//删除左右两端的空格
function trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}